# coding: utf-8
# author: t_wolf

import os
import re
import sys
import web
import json
import time
import sqlite3
from functools import wraps
from datetime import datetime

from web.session import Session
from threading import Timer
from configure import *

# function call
CURR_FUNC_POSITION = 0
CURR_FILE_POSITION = 1
CALL_FILE_POSITION = 2

get_frame = sys._getframe
ROOT_PATH = os.path.abspath(os.curdir)


def run_jar():
    pass


def get_abs_path(position):
    return get_frame(position).f_code.co_filename


def get_current_time(time_format='%y-%m-%d %H:%M:%S'):
    """
    :return: time format by 'timeFormat'.
    """
    return datetime.now().strftime(time_format)


def inject_object_by_dict(obj, obj_dict):
    for _key in obj_dict.keys():
        setattr(obj, _key, obj_dict[_key])
    return obj


def iter_factor(iter_obj):
    if isinstance(iter_obj, int):
        iter_obj = xrange(iter_obj)
    for _val in iter_obj:
        yield _val


class LOGGER:
    __LOG_LEVEL = {
        'INFO': 0,
        'DEBUG': 1,
        'WARNING': 2,
        'ERROR': 3,
        'CRITICAL': 4
    }

    def __init__(self, level):
        self.__log_num = self.__LOG_LEVEL[level]

    def __write_log(func):
        def wrapper(self, _str):
            if self.__LOG_LEVEL[func.__name__.upper()] >= self.__log_num:
                log_str = "[%s] <%s:%d> @ [%s]: %s" % (
                    get_current_time("%y-%m-%d %H:%M:%S-%f"),
                    get_frame(CURR_FILE_POSITION).f_code.co_filename.split("%s/" % ROOT_PATH)[1],
                    get_frame(CURR_FUNC_POSITION).f_back.f_lineno,
                    func.__name__.upper(),
                    _str
                )
                sys.stdout.write("%s%s" % (log_str, os.linesep))
                os.system("echo \"%s\" >> %s" % (log_str, CONFIGS.LOG_NAME))

        return wrapper

    @__write_log
    def info(self, log_str): pass

    @__write_log
    def debug(self, log_str): pass

    @__write_log
    def error(self, log_str): pass

    @__write_log
    def warning(self, log_str): pass

    @__write_log
    def critical(self, log_str): pass


def json2params(str_obj):
    return json.load(str_obj)


def params2json(str_obj):
    pass


def xml2params(xml_obj):
    pass


def params2xml(xml_obj):
    pass


if __name__ == '__main__':
    a = iter_factor(1000)
    print a.next()
    print a.next()
